﻿using System;
using System.Collections.Generic;
using System.Text;
using NetworkLib.Utilities;
using PokemonBattle.BattleNetwork;

namespace PokemonBattle.BattleRoom.Client
{
    public static class RoomClientHelper
    {
        public static ByteArray AcceptChallenge(int targetIdentity)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_AcceptChallenge_Hash);
            byteArray.WriteInt(targetIdentity);


            return byteArray;
        }

        public static ByteArray RefuseChallenge(int targetIdentity)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_RefuseChallenge_Hash);
            byteArray.WriteInt(targetIdentity);


            return byteArray;
        }

        public static ByteArray Challenge(int targetIdentity, ChallengeInfo info)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_Challenge_Hash);
            byteArray.WriteInt(targetIdentity);

            info.Write(byteArray);


            return byteArray;
        }

        public static ByteArray CancelChallenge(int targetIdentity)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_CancelChallenge_Hash);
            byteArray.WriteInt(targetIdentity);


            return byteArray;
        }

        public static ByteArray StartBattle(int with, ChallengeInfo info)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_StartBattle_Hash);
            byteArray.WriteInt(with);

            info.Write(byteArray);


            return byteArray;
        }

        public static ByteArray StartFourPlayerBattle(int battleIdentity, byte position)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_StartFourPlayerBattle_Hash);
            byteArray.WriteInt(battleIdentity);

            byteArray.WriteByte(position);


            return byteArray;
        }

        public static ByteArray GetObserveInfo(int identity)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_GetObserveInfo_Hash);
            byteArray.WriteInt(identity);


            return byteArray;
        }

        public static ByteArray UserLogon(User info)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_UserLogon_Hash);
            info.Write(byteArray);


            return byteArray;
        }

        public static ByteArray UserLogout()
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_UserLogout_Hash);

            return byteArray;
        }

        public static ByteArray UpdateUser(User info)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_UpdateUser_Hash);
            info.Write(byteArray);


            return byteArray;
        }

        public static ByteArray ReceiveBroadcastMessage(string message)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_ReceiveBroadcastMessage_Hash);
            byteArray.WriteUTF(message);


            return byteArray;
        }

        public static ByteArray ReceiveChatMessage(int to, string message)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_ReceiveChatMessage_Hash);
            byteArray.WriteInt(to);

            byteArray.WriteUTF(message);


            return byteArray;
        }

        public static ByteArray ReceiveRoomCommand(string message)
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_ReceiveRoomCommand_Hash);
            byteArray.WriteUTF(message);


            return byteArray;
        }

        public static ByteArray RegistFourPlayer()
        {
            ByteArray byteArray = new ByteArray();

            byteArray.WriteInt(EnumRoomMethods.Server_RegistFourPlayer_Hash);

            return byteArray;
        }

        #region IRoomClientService

        public static bool InterpretMessage(int sessionID, ByteArray byteArray, IRoomClientService clientService)
        {
            byteArray.BypassHeader();
            int methodID = byteArray.ReadInt();

            switch (methodID)
            {
                case EnumRoomMethods.Client_AcceptChallenge_Hash:
                    return OnAcceptChallenge(byteArray, clientService);

                case EnumRoomMethods.Client_RefuseChallenge_Hash:
                    return OnRefuseChallenge(byteArray, clientService);

                case EnumRoomMethods.Client_Challenge_Hash:
                    return OnChallenge(byteArray, clientService);

                case EnumRoomMethods.Client_CancelChallenge_Hash:
                    return OnCancelChallenge(byteArray, clientService);

                case EnumRoomMethods.Client_DirectBattle_Hash:
                    return OnDirectBattle(byteArray, clientService);

                case EnumRoomMethods.Client_AgentBattle_Hash:
                    return OnAgentBattle(byteArray, clientService);

                case EnumRoomMethods.Client_ReceiveObserveInfo_Hash:
                    return OnReceiveObserveInfo(byteArray, clientService);

                case EnumRoomMethods.Client_ReceiveBroadcastMessage_Hash:
                    return OnReceiveBroadcastMessage(byteArray, clientService);

                case EnumRoomMethods.Client_ReceiveChatMessage_Hash:
                    return OnReceiveChatMessage(byteArray, clientService);

                case EnumRoomMethods.Client_LogonSuccess_Hash:
                    return OnLogonSuccess(byteArray, clientService);

                case EnumRoomMethods.Client_LogonFail_Hash:
                    return OnLogonFail(byteArray, clientService);

                case EnumRoomMethods.Client_BeKicked_Hash:
                    return OnBeKicked(byteArray, clientService);

                case EnumRoomMethods.Client_UpdateUser_Hash:
                    return OnUpdateUser(byteArray, clientService);

                case EnumRoomMethods.Client_AddNewUser_Hash:
                    return OnAddNewUser(byteArray, clientService);

                case EnumRoomMethods.Client_RemoveUser_Hash:
                    return OnRemoveUser(byteArray, clientService);

                case EnumRoomMethods.Client_AddUserList_Hash:
                    return OnAddUserList(byteArray, clientService);

                case EnumRoomMethods.Client_UpdateRoomSetting_Hash:
                    return OnUpdateRoomSetting(byteArray, clientService);

                case EnumRoomMethods.Client_RegistFourPlayerSuccess_Hash:
                    return OnRegistFourPlayerSuccess(byteArray, clientService);

                case EnumRoomMethods.Client_AddFourPlayerRoomList_Hash:
                    return OnAddFourPlayerRoomList(byteArray, clientService);

                case EnumRoomMethods.Client_AddFourPlayerRoom_Hash:
                    return OnAddFourPlayerRoom(byteArray, clientService);

                case EnumRoomMethods.Client_RemoveFourPlayerRoom_Hash:
                    return OnRemoveFourPlayerRoom(byteArray, clientService);

                case EnumRoomMethods.Client_UpdateFourPlayerRoom_Hash:
                    return OnUpdateFourPlayerRoom(byteArray, clientService);
            }

            byteArray.Rewind();
            return false;
        }

        private static bool OnAcceptChallenge(ByteArray byteArray, IRoomClientService clientService)
        {
            int from = byteArray.ReadInt();

            clientService.OnAcceptChallenge(from);
            return true;
        }

        private static bool OnRefuseChallenge(ByteArray byteArray, IRoomClientService clientService)
        {
            int from = byteArray.ReadInt();

            clientService.OnRefuseChallenge(from);
            return true;
        }

        private static bool OnChallenge(ByteArray byteArray, IRoomClientService clientService)
        {
            int from = byteArray.ReadInt();
            ChallengeInfo info = new ChallengeInfo();
            info.Read(byteArray);

            clientService.OnChallenge(from, info);
            return true;
        }

        private static bool OnCancelChallenge(ByteArray byteArray, IRoomClientService clientService)
        {
            int from = byteArray.ReadInt();

            clientService.OnCancelChallenge(from);
            return true;
        }

        private static bool OnDirectBattle(ByteArray byteArray, IRoomClientService clientService)
        {
            int server = byteArray.ReadInt();
            BattleMode battleMode = BattleModeHelper.ReadFromByteArray(byteArray);

            clientService.OnDirectBattle(server, battleMode);
            return true;
        }

        private static bool OnAgentBattle(ByteArray byteArray, IRoomClientService clientService)
        {
            int identity = byteArray.ReadInt();
            byte playerPosition = byteArray.ReadByte();
            BattleMode battleMode = BattleModeHelper.ReadFromByteArray(byteArray);

            clientService.OnAgentBattle(identity, playerPosition, battleMode);
            return true;
        }

        private static bool OnReceiveObserveInfo(ByteArray byteArray, IRoomClientService clientService)
        {
            ObserveInfo info = new ObserveInfo();
            info.Read(byteArray);

            clientService.OnReceiveObserveInfo(info);
            return true;
        }

        private static bool OnReceiveBroadcastMessage(ByteArray byteArray, IRoomClientService clientService)
        {
            string message = byteArray.ReadUTF();

            clientService.OnReceiveBroadcastMessage(message);
            return true;
        }

        private static bool OnReceiveChatMessage(ByteArray byteArray, IRoomClientService clientService)
        {
            int from = byteArray.ReadInt();
            string message = byteArray.ReadUTF();

            clientService.OnReceiveChatMessage(from, message);
            return true;
        }

        private static bool OnLogonSuccess(ByteArray byteArray, IRoomClientService clientService)
        {
            User info = new User();
            info.Read(byteArray);

            clientService.OnLogonSuccess(info);
            return true;
        }

        private static bool OnLogonFail(ByteArray byteArray, IRoomClientService clientService)
        {
            string message = byteArray.ReadUTF();

            clientService.OnLogonFail(message);
            return true;
        }

        private static bool OnBeKicked(ByteArray byteArray, IRoomClientService clientService)
        {

            clientService.OnBeKicked();
            return true;
        }

        private static bool OnUpdateUser(ByteArray byteArray, IRoomClientService clientService)
        {
            User userInfo = new User();
            userInfo.Read(byteArray);

            clientService.OnUpdateUser(userInfo);
            return true;
        }

        private static bool OnAddNewUser(ByteArray byteArray, IRoomClientService clientService)
        {
            User userInfo = new User();
            userInfo.Read(byteArray);

            clientService.OnAddNewUser(userInfo);
            return true;
        }

        private static bool OnRemoveUser(ByteArray byteArray, IRoomClientService clientService)
        {
            int identity = byteArray.ReadInt();

            clientService.OnRemoveUser(identity);
            return true;
        }

        private static bool OnAddUserList(ByteArray byteArray, IRoomClientService clientService)
        {
            UserSequence users = new UserSequence();
            users.Read(byteArray);

            clientService.OnAddUserList(users);
            return true;
        }

        private static bool OnUpdateRoomSetting(ByteArray byteArray, IRoomClientService clientService)
        {
            RoomBattleSetting setting = new RoomBattleSetting();
            setting.Read(byteArray);

            clientService.OnUpdateRoomSetting(setting);
            return true;
        }

        private static bool OnRegistFourPlayerSuccess(ByteArray byteArray, IRoomClientService clientService)
        {
            int identity = byteArray.ReadInt();

            clientService.OnRegistFourPlayerSuccess(identity);
            return true;
        }

        private static bool OnAddFourPlayerRoomList(ByteArray byteArray, IRoomClientService clientService)
        {
            FourPlayerRoomSequence rooms = new FourPlayerRoomSequence();
            rooms.Read(byteArray);

            clientService.OnAddFourPlayerRoomList(rooms);
            return true;
        }

        private static bool OnAddFourPlayerRoom(ByteArray byteArray, IRoomClientService clientService)
        {
            int identity = byteArray.ReadInt();
            string host = byteArray.ReadUTF();

            clientService.OnAddFourPlayerRoom(identity, host);
            return true;
        }

        private static bool OnRemoveFourPlayerRoom(ByteArray byteArray, IRoomClientService clientService)
        {
            int identity = byteArray.ReadInt();

            clientService.OnRemoveFourPlayerRoom(identity);
            return true;
        }

        private static bool OnUpdateFourPlayerRoom(ByteArray byteArray, IRoomClientService clientService)
        {
            int identity = byteArray.ReadInt();
            byte userCount = byteArray.ReadByte();

            clientService.OnUpdateFourPlayerRoom(identity, userCount);
            return true;
        }

        #endregion
    }
}
